package com.deve.pig.service.merchant.impl;

import com.deve.pig.common.BusinessData;
import com.deve.pig.mapper.MerchantBranchStoreMapper;
import com.deve.pig.model.MerchantBranchStore;
import com.deve.pig.model.MerchantBranchStoreExample.Criteria;
import com.deve.pig.model.MerchantBranchStoreExample;
import com.deve.pig.service.merchant.IMerchantBranchStoreService;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;

import my.utils.Pagination;
import org.springframework.stereotype.Service;

@Service("merchantBranchStoreService")
public class MerchantBranchStoreServiceImpl extends BusinessData implements IMerchantBranchStoreService {
    @Resource
    private MerchantBranchStoreMapper merchantBranchStoreMapper;

    @Override
    public int add(MerchantBranchStore entity) {
        return merchantBranchStoreMapper.insert(entity);
    }

    @Override
    public int delete(MerchantBranchStoreExample example) {
        return merchantBranchStoreMapper.deleteByExample(example);
    }

    @Override
    public int delete(int id) {
        return merchantBranchStoreMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(MerchantBranchStore entity) {
        return merchantBranchStoreMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public Pagination findPagination(Map<String, Object> queryMap, int currentPage, int pageSize) {
        MerchantBranchStoreExample merchantBranchStoreExample = new MerchantBranchStoreExample();
        merchantBranchStoreExample.setOrderByClause("id desc");
        Criteria criteria = merchantBranchStoreExample.createCriteria();
        criteria.andMerchantIdEqualTo(Integer.parseInt((String) queryMap.get("merchantId")));
        // 设置搜索条件参数
        if (queryMap != null) {
            if (queryMap.containsKey("name")) {
                criteria.andNameLike("%" + (String) queryMap.get("name") + "%");
            }
        }
        // 设置分页参数
        merchantBranchStoreExample.setPageSize(pageSize);
        merchantBranchStoreExample.setStartIndex((currentPage - 1) * pageSize);
        List<MerchantBranchStore> items = merchantBranchStoreMapper.selectByExample(merchantBranchStoreExample);
        int totalCount = (int) merchantBranchStoreMapper.countByExample(merchantBranchStoreExample);
        return new Pagination(pageSize, currentPage, totalCount, items);
    }

    @Override
    public MerchantBranchStore findById(int id) {
        return merchantBranchStoreMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<MerchantBranchStore> findAll() {
        return merchantBranchStoreMapper.selectByExample(null);
    }

    @Override
    public List<MerchantBranchStore> findByCriteria(MerchantBranchStoreExample criteria) {
        return merchantBranchStoreMapper.selectByExample(criteria);
    }

    @Override
    @SuppressWarnings("unchecked")
    public LinkedHashMap<String, List> getExportData() {
        LinkedHashMap<String, List> map = new LinkedHashMap<String, List>();
        map.put("SheetName_1", findAll());
        map.put("SheetName_2", findAll());
        map.put("SheetName_3", findAll());
        map.put("SheetName_N", findAll());
        return map;
    }

    @Override
    public int countByExample(MerchantBranchStoreExample example) {
        return merchantBranchStoreMapper.countByExample(example);
    }
}